/*
 * Copyright (C) 2012, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import xowl.gmi.environment.Locale;
import xowl.utils.config.Configuration;

/**
 *
 * @author Laurent WOUTERS
 */
public class DialogOptions extends javax.swing.JDialog {
    private static int width = 400;
    private static int height = 170;
    
    private boolean accept;
    private javax.swing.JComboBox comboWorkbench;
    private javax.swing.JComboBox comboLocale;
    private Map<String, String> workbenchNames;
    private Map<String, String> localeIDs;
    private Map<String, String> localeNames;
    private Map<String, javax.swing.ImageIcon> localeIcons;
    
    public boolean isAccept() { return accept; }
    public String getSelectedWorkbench() { return (String)comboWorkbench.getSelectedItem(); }
    public String getSelectedLocale() { return (String)comboLocale.getSelectedItem(); }
    
    public DialogOptions(Locale locale) {
        this.accept = false;
        this.workbenchNames = new HashMap<>();
        this.localeIDs = new HashMap<>();
        this.localeNames = new HashMap<>();
        this.localeIcons = new HashMap<>();
        initComponents(locale);
    }
    
    public void searchWorkbenches(String dir, String wbn) { search(wbn, new java.io.File(dir)); }
    private void search(String fn, java.io.File file) {
        if (file.isDirectory()) {
            java.io.File[] content = file.listFiles();
            for (java.io.File f : content)
                search(fn, f);
        } else if (fn.equals(file.getName())) {
            search(file);
        }
    }
    private void search(java.io.File wbf) {
        Configuration config = new Configuration();
        try {
            config.load(wbf.getPath());
            String path = wbf.getPath();
            String name = config.getValue("name");
            if ("\\".equals(java.io.File.separator))
                path = path.replace("\\", "/");
            workbenchNames.put(path, name);
        } catch (IOException ex) { }
    }
    
    public void addLocale(String id, String name, String icon) {
        localeIDs.put(name, id);
        localeNames.put(id, name);
        localeIcons.put(id, IconProvider.getEmbedded(icon));
    }
    public void prepare(String defaultLocale) {
        for (String wb : workbenchNames.keySet())
            comboWorkbench.addItem(wb);
        comboWorkbench.setSelectedIndex(0);
        List<String> names = new ArrayList<>(localeNames.values());
        Collections.sort(names);
        for (String name : names)
            comboLocale.addItem(localeIDs.get(name));
        comboLocale.setSelectedItem(defaultLocale);
    }
    
    private void initComponents(Locale locale) {
        java.awt.Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width-width)/2;
        int y = (screen.height-height)/2;
        this.setBounds(x, y, width, height);
        this.setModalityType(javax.swing.JDialog.ModalityType.APPLICATION_MODAL);
        this.setResizable(false);
        this.setTitle(locale.appOptions);
        this.setIconImage(IconProvider.getEmbedded(locale.optionsMenuItem_icon).getImage());
        
        javax.swing.JLabel labelWorkbench = new javax.swing.JLabel(locale.appWorkbench, IconProvider.getEmbedded("workbench.png"), javax.swing.SwingConstants.LEFT);
        labelWorkbench.setMinimumSize(new java.awt.Dimension(100, 25));
        labelWorkbench.setPreferredSize(new java.awt.Dimension(100, 25));
        labelWorkbench.setMaximumSize(new java.awt.Dimension(100, 25));
        
        comboWorkbench = new javax.swing.JComboBox<>();
        comboWorkbench.setMinimumSize(new java.awt.Dimension(200, 25));
        comboWorkbench.setPreferredSize(new java.awt.Dimension(200, 25));
        comboWorkbench.setMaximumSize(new java.awt.Dimension(200, 25));
        comboWorkbench.setRenderer(new javax.swing.DefaultListCellRenderer(){
            @Override public java.awt.Component getListCellRendererComponent(javax.swing.JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                javax.swing.JLabel label = (javax.swing.JLabel)super.getListCellRendererComponent(list, workbenchNames.get((String)value), index, isSelected, cellHasFocus);
                label.setIcon(localeIcons.get((String)value));
                return label;
            }
        });
        
        javax.swing.JLabel labelLocale = new javax.swing.JLabel(locale.appLocale, IconProvider.getEmbedded("locale.png"), javax.swing.SwingConstants.LEFT);
        labelLocale.setMinimumSize(new java.awt.Dimension(100, 25));
        labelLocale.setPreferredSize(new java.awt.Dimension(100, 25));
        labelLocale.setMaximumSize(new java.awt.Dimension(100, 25));
        
        comboLocale = new javax.swing.JComboBox<>();
        comboLocale.setMinimumSize(new java.awt.Dimension(200, 25));
        comboLocale.setPreferredSize(new java.awt.Dimension(200, 25));
        comboLocale.setMaximumSize(new java.awt.Dimension(200, 25));
        comboLocale.setRenderer(new javax.swing.DefaultListCellRenderer(){
            @Override public java.awt.Component getListCellRendererComponent(javax.swing.JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                javax.swing.JLabel label = (javax.swing.JLabel)super.getListCellRendererComponent(list, localeNames.get((String)value), index, isSelected, cellHasFocus);
                label.setIcon(localeIcons.get((String)value));
                return label;
            }
        });
        
        javax.swing.JButton buttonOK = new javax.swing.JButton(locale.ok_text);
        buttonOK.setMinimumSize(new java.awt.Dimension(75, 25));
        buttonOK.setPreferredSize(new java.awt.Dimension(75, 25));
        buttonOK.setMaximumSize(new java.awt.Dimension(75, 25));
        buttonOK.addActionListener(new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                if (comboWorkbench.getSelectedItem() == null)
                    return;
                if (comboLocale.getSelectedItem() == null)
                    return;
                accept = true;
                setVisible(false);
            }
        });
        getRootPane().setDefaultButton(buttonOK);
        
        javax.swing.JButton buttonCancel = new javax.swing.JButton(locale.cancel_text);
        buttonCancel.setMinimumSize(new java.awt.Dimension(75, 25));
        buttonCancel.setPreferredSize(new java.awt.Dimension(75, 25));
        buttonCancel.setMaximumSize(new java.awt.Dimension(75, 25));
        buttonCancel.addActionListener(new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                accept = false;
                setVisible(false);
            }
        });
        getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0), "Cancel");
        getRootPane().getActionMap().put("Cancel", new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                accept = false;
                setVisible(false);
            }
        });
        
        javax.swing.JPanel row1 = new javax.swing.JPanel();
        row1.setLayout(new javax.swing.BoxLayout(row1, javax.swing.BoxLayout.X_AXIS));
        row1.add(javax.swing.Box.createHorizontalStrut(10));
        row1.add(labelWorkbench);
        row1.add(javax.swing.Box.createHorizontalStrut(10));
        row1.add(comboWorkbench);
        row1.add(javax.swing.Box.createHorizontalStrut(10));
        
        javax.swing.JPanel row2 = new javax.swing.JPanel();
        row2.setLayout(new javax.swing.BoxLayout(row2, javax.swing.BoxLayout.X_AXIS));
        row2.add(javax.swing.Box.createHorizontalStrut(10));
        row2.add(labelLocale);
        row2.add(javax.swing.Box.createHorizontalStrut(10));
        row2.add(comboLocale);
        row2.add(javax.swing.Box.createHorizontalStrut(10));
        
        javax.swing.JPanel row3 = new javax.swing.JPanel();
        row3.setLayout(new javax.swing.BoxLayout(row3, javax.swing.BoxLayout.X_AXIS));
        row3.add(javax.swing.Box.createHorizontalStrut(10));
        row3.add(buttonCancel);
        row3.add(javax.swing.Box.createHorizontalGlue());
        row3.add(buttonOK);
        row3.add(javax.swing.Box.createHorizontalStrut(10));
        
        javax.swing.JPanel content = (javax.swing.JPanel)getContentPane();
        content.setLayout(new javax.swing.BoxLayout(content, javax.swing.BoxLayout.Y_AXIS));
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(row1);
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(row2);
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(row3);
        content.add(javax.swing.Box.createVerticalStrut(10));
    }
}
